package org.lib.mysqlhello.common;

/**
 * 请求统一返回值
 * @author ben
 * @date 2021-08-21 23:02:04 CST
 * @param <T>
 */
public final class ResultVO<T> {

	/**
	 * 响应码：200为正常，其它非正常
	 */
	private Integer code;
	
	/**
	 * 响应内容描述
	 */
	private String message;
	
	/**
	 * 响应数据，可能为null
	 */
	private T data;

	// ---构造函数---
	
	public ResultVO() {
		this.code = 200;
		this.message = "OK";
	}
	
	public ResultVO(Integer code, String message) {
		this.code = code;
		this.message = message;
	}
	
	public ResultVO(Integer code, String message, T data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}
	
	// ---静态方法
	
	/**
	 * 请求成功
	 * @author ben
	 * @date 2021-08-21 23:04:22 CST
	 * @param <T>
	 * @param data
	 * @return
	 */
	public static <T> ResultVO<T> getSuccess(T data) {
		ResultVO<T> ret = new ResultVO<T>();
		ret.setData(data);
		return ret;
	}

	/**
	 * 请求失败
	 * @author ben
	 * @date 2021-08-21 23:04:29 CST
	 * @param <T>
	 * @param code
	 * @param message
	 * @param data
	 * @return
	 */
	public static <T> ResultVO<T> getFailed(Integer code, String message, T data) {
		return new ResultVO<T>(code, message, data);
	}
	
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	
	@Override
	public String toString() {
		return "ResultVO [code=" + code + ", message=" + message + ", data=" + data + "]";
	}
	
}
